package com.yanqu.road.server.cmd.marriage.cross;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.MarriageModule;
import com.yanqu.road.server.manger.CrossMarriageGroupMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_MARRIAGE_REFRESH_SERVER_APPLY_LIST, desc = "刷新跨服非指向联姻列表")
public class CrossMarriageRefreshServerApplyCmd extends AbstractCommand {

    private int childSex;
    private String childUid;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageRefreshServerApplyReqMsg reqMsg = CrossMarriageProto.CrossMarriageRefreshServerApplyReqMsg.parseFrom(packet.getBytes());
        childSex = reqMsg.getChildSex();
        childUid = reqMsg.getChildUid();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!CrossMarriageGroupMgr.canCrossMarriage(GameServer.getInstance().getServerId())){
            ret = GameErrorCode.E_CROSS_MARRIAGE_NO_OPEN;
            return;
        }
        ret = player.getModule(MarriageModule.class).refreshCrossServerApply();
        if(0 != ret){
            return;
        }
        BigInteger minEarnSpeed = BigInteger.ZERO;
        BigInteger maxEarnSpeed = BigInteger.ZERO;
        if(!StringUtils.isNullOrEmpty(childUid)){
            MarriageInfo marriageInfo = MarriageMgr.getAdultChild(player.getUserId(), childUid);
            if(null != marriageInfo) {
                BigInteger childEarnSpeed = marriageInfo.getChildEarnSpeed();
                minEarnSpeed = new BigDecimal(childEarnSpeed).multiply(BigDecimal.valueOf(GameConfig.CROSS_MARRIAGE_REFRESH_MIN_CHILD_EARNSPEED)).
                        divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).toBigInteger();
                maxEarnSpeed = new BigDecimal(childEarnSpeed).multiply(BigDecimal.valueOf(GameConfig.CROSS_MARRIAGE_REFRESH_MAX_CHILD_EARNSPEED)).
                        divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).toBigInteger();
                if(minEarnSpeed.compareTo(maxEarnSpeed) > 0){
                    maxEarnSpeed = minEarnSpeed;
                }
            }
        }
        CrossMarriageProto.CrossMarriageRefreshServerApplyReqMsg.Builder getMsg = CrossMarriageProto.CrossMarriageRefreshServerApplyReqMsg.newBuilder();
        getMsg.setChildSex(childSex);
        getMsg.setMinEarnSpeed(minEarnSpeed.toString());
        getMsg.setMaxEarnSpeed(maxEarnSpeed.toString());
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_MARRIAGE_REFRESH_SERVER_APPLY, getMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            CrossMarriageProto.CrossMarriageRefreshServerApplyRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageRefreshServerApplyRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CROSS_MARRIAGE_REFRESH_SERVER_APPLY_LIST, respMsg);
        }
    }
}
